
/*
 * =====================================================================================
 *       Filename:  RequestSession.cc
 *
 *    Description:
 *
 *
 *        Version:  1.0
 *        Created:  03/06/19 15:59:09
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  liushuyong (), shuyong1999@163.com
 *        Company:  shuyong
 *
 * =====================================================================================
 */
#include <iostream>
#include <sstream>
#include <codecvt>

#include "RequestSession.h"
#include "SessionCache.h"
#include "CommFunc.h"
#include "TTSEngineMgr.h"

#define PRIOR_KEY "synthesis"
#define PRIOR_TOKEN "d1bd6e9f-8dbf-4ea6-b92f-75dfa263304c"

DECLARE_bool(use_repair_tools); //修复工具
DECLARE_int32(max_text_len);

RequestSession::RequestSession(double engineParam[], bool isNotStream) {
#ifdef Debug
    //    LOG(INFO) << "New" << endl;
#endif
    int aue = engineParam[EXTRACTOR_INDEX]; // wav, pcm, opus
    int text_type = (int) engineParam[TEXT_TYPE_INDEX];

    switch (aue) {
    case 0:
        mTTSEngineParam.voice_type = 0;
        break;

    case 1:
        mTTSEngineParam.voice_type = 1;
        break;

    case 2:
        mTTSEngineParam.voice_type = 2;
        break;

    case 3:
        mTTSEngineParam.voice_type = 3;
        break;

    default:
        mTTSEngineParam.voice_type = 1;
        break;
    }

    mTTSEngineParam.text_type = 0;  // wether the text is ssml
    mTTSEngineParam.sample_rate = (int) engineParam[SAMPLE_RATE_INDEX]; //[4000, 24000]
    mTTSEngineParam.volume = engineParam[VOLUME_INDEX]; // [0.1, 10.0]
    mTTSEngineParam.speed = engineParam[SPEED_INDEX];   // [0.5, 2.0]
    mTTSEngineParam.model_index = (int) engineParam[TIMBRE_INDEX];  // [0, 1], currently we have 2 timbre
    mTTSEngineParam.return_all = isNotStream;
    mspTTSEngineMgr = make_shared < TTSEngineMgr > ();
    mspTTSEngineMgr->CreateTTSTask(&mTTSEngineParam);
}

RequestSession::~RequestSession() {
#ifdef Debug
    //    LOG(INFO) << "Del" << endl;
#endif

    if (mspTTSEngineMgr) {
        mspTTSEngineMgr.reset();
    }
}

void RequestSession::doSynthetize(RPCContextSPtr& rpcCtx) {
    mspTTSEngineMgr->startAsyncTask(rpcCtx);
}

void RequestSession::readCache(RPCContextSPtr& rpcCtx) {
    mspTTSEngineMgr->readCache(rpcCtx, false);
}
